import router from '../router';
import { fetchRoutes } from '../api/routes';

/**
 * 将接口返回的字符串组件路径转换为异步加载函数
 */
function loadComponent(componentPath: string) {
    return () => import(`../../${componentPath}`);
}

/**
 * 加载并注册动态路由
 */
export async function loadDynamicRoutes() {
    const routeData = await fetchRoutes();

    routeData.forEach(route => {
        const dynamicRoute = {
            ...route,
            component: loadComponent(route.component),
        };
        router.addRoute(dynamicRoute);
    });
}
